package com.zuozhj.controller;

import org.springframework.web.bind.annotation.RestController;

import com.zuozhj.controller.PaymentService.PaymentService;
import com.zuozhj.controller.PaymentService.SupporType;
import com.zuozhj.enums.paymentEnums;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@RestController
public class PaymentController {
    
    private Map<String, PaymentService> paymentServiceMap;

    @GetMapping("test/{type}")
    public String pay(@PathVariable("type") String type) {
        PaymentService paymentService = paymentServiceMap.getOrDefault(type, paymentServiceMap.get(paymentEnums.CASH.name()));
        System.out.println(type);
        return paymentService.pay();
    }
    

    @Autowired
    public void setPaymentServiceMap(List<PaymentService> paymentService) {
        this.paymentServiceMap = paymentService.stream()
            .filter(service -> null != service.getClass() && null != service.getClass().getAnnotation(SupporType.class))
            .collect(Collectors.toMap(
                service -> service.getClass().getAnnotation(SupporType.class).value().name(),
                service -> service
            ));
    }
}
